package com.gyx.lottery.controller;

import com.gyx.lottery.common.pojo.CommonResult;
import com.gyx.lottery.common.utils.JacksonUtil;
import com.gyx.lottery.controller.param.DrawPrizeParam;
import com.gyx.lottery.controller.param.WinningRecordParam;
import com.gyx.lottery.controller.result.WinningRecordsResult;
import com.gyx.lottery.service.DrawPrizeService;
import com.gyx.lottery.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {
    /**
     * 日志打印
     */
    Logger logger= LoggerFactory.getLogger(DrawPrizeController.class);
    @Autowired
    DrawPrizeService drawPrizeService;
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean>drawPrize(@RequestBody @Validated DrawPrizeParam param){
        logger.info("drawPrize DrawPrizeParam:{}",param);
        drawPrizeService.drawPrize(param);
        return CommonResult.success(Boolean.TRUE);
    }
    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordsResult>> findWinningRecord
            (@RequestBody @Validated WinningRecordParam param){
        logger.info("findWinningRecord :WinningRecordParam{}", JacksonUtil.writeVauleAsString(param));
        List<WinningRecordDTO> winningRecordDTOList=drawPrizeService.getWinningRecord(param);
        return CommonResult.success(convertToWinningRecordsResult(winningRecordDTOList));
    }

    private List<WinningRecordsResult> convertToWinningRecordsResult
            (List<WinningRecordDTO> winningRecordDTOList) {
        List<WinningRecordsResult> results=winningRecordDTOList.stream()
                .map(winningRecordDTO -> {
                    WinningRecordsResult winningRecordsResult=new WinningRecordsResult();
                    winningRecordsResult.setWinnerId(winningRecordDTO.getWinnerId());
                    winningRecordsResult.setWinnerName(winningRecordDTO.getWinnerName());
                    winningRecordsResult.setPrizeName(winningRecordDTO.getPrizeName());
                    winningRecordsResult.setPrizeTier(winningRecordDTO.getPrizeTier().getMessage());
                    winningRecordsResult.setWinningTime(winningRecordDTO.getWinningTime());
                    return  winningRecordsResult;
                }).collect(Collectors.toList());
        return results;
    }
}
